//
//  YZMNavigationTitleView.m
//  YZMicroStock
//
//  Created by wangminhong on 16/5/30.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMNavigationTitleView.h"

@interface YZMNavigationTitleView ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *arrowImageView;

@end

@implementation YZMNavigationTitleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame: frame];
    if (self) {
        [self createObjects];
        [self assignConstraints];
    }
    return self;
}

- (void)createObjects
{
    self.titleLabel = [[UILabel alloc] init];
    [self addSubview: self.titleLabel];
    self.titleLabel.font = [YZMAppStyle largeSizeFont];
    self.titleLabel.textColor = [UIColor whiteColor];
    
    self.arrowImageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"white_arrow_down"]];
    [self addSubview: self.arrowImageView];
}

- (void)assignConstraints
{
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).with.offset(-6);
//        make.centerY.equalTo(self);
        make.top.bottom.equalTo(self);
    }];
    
    [self.arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_right).offset(4);
        make.centerY.equalTo(self.titleLabel).offset(1);
    }];
}

- (void)updateConstraints
{
    if (self.superview) {
        [self updateConstraintsBasicSupview];
    }
    [super updateConstraints];
}

- (void)updateConstraintsBasicSupview
{
    UIScreen *mainScreen = [UIScreen mainScreen];
    CGFloat offset = mainScreen.bounds.size.width/2 - self.frame.origin.x;
    if (self.style == 1) {
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.mas_leading).with.offset(offset).priorityHigh();
        }];
    }
    else
    {
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_leading).with.offset(-4+offset).priorityHigh();
        }];
    }
    
}


#pragma mark - eration

- (void)setTitle:(NSString *)title
{
    self.titleLabel.text = title;
}

- (void)setStyle:(NSInteger)style
{
    _style = style;
    self.arrowImageView.hidden = (style == 1);
    [self updateConstraintsBasicSupview];
}


- (void)arrowUp
{
    self.arrowImageView.image = [UIImage imageNamed: @"white_arrow_up"];
    self.showFlag = YES;
}

- (void)arrowDown
{
    self.arrowImageView.image = [UIImage imageNamed: @"white_arrow_down"];
    self.showFlag = NO;
}


@end
